﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlainOldVersionControl.Data;
using PlainOldVersionControl.Data.Base;

namespace PlainOldVersionControl.BusinessLogic
{
    public abstract class BaseBL
    {
        #region Constructor

        static BaseBL()
        {

        }

        internal BaseBL(object b)
        {
            DomainObject = b;
        }

        #endregion

        #region Properties

        protected object DomainObject;        

        #endregion

        #region Public Methods

        public abstract void Validate();

        public virtual void Save()
        {
            ValidateBase();
            Validate();
            DA.Save(DomainObject);
        }

        public virtual void Delete()
        {
            DA.Delete(DomainObject);
        }        
            

        #endregion

        #region Private Methods

        protected void ValidateBase()
        {
            if (DomainObject == null)
            {
                throw new InvalidOperationException(
                    "Do not call validate on a null domain object.  Instead do a null check if nulls are allow, then call validate.");
            }

        }

        #endregion

        #region Static Methods and Properties

        public static string GetVersionRoot()
        {
            using (Tx.Start())
            {
                return PlainSettingsDA.GetRootPath();
            }
        }

        internal static DateTime SqlMinDate
        {
            get
            {
                return _sqlMinDate;
            }
        }
        internal static void BuildFactory()
        {
            DA.BuildFactory();
        }
        protected static IList<T> GetAll<T>()
        {
            return DA.GetAll<T>();
        }
        protected static T GetById<T>(string hash)
        {
            return DA.GetById<T>(hash);
        }
        protected static T LoadById<T>(int id)
        {
            return DA.LoadByID<T>(id);
        }
        private static readonly DateTime _sqlMinDate = new DateTime(1800, 1, 1);

        #region Public Static Methods              

        #endregion

        #endregion

        #region Helpers
        
        private string GetClass()
        {
            string retVal = this.ToString();
            while (retVal.Contains("."))
            {
                retVal = retVal.Substring(retVal.IndexOf(".") + 1);
            }
            retVal = retVal.Replace("BL", "");
            return retVal;
        }

        #endregion  
    }
}
